/*bC6PlMPj1z4dd3YE5o/v0g0kPVRUE18Ro7GsIADPKKLPhkPmwZoPURbQgyBRC/4H*/
/**
 * 向导批改
 * @author yinshb
 */
import React, { Component } from 'react';
import { createPage, base,ajax,toast } from 'nc-lightapp-front';
let { NCTabs, NCButton, NCModal, NCStep } = base;
import  Utils from '../../../public/utils';
import Batcheditform from './BatchEditForm';
import BatchEditFilter from './BatchEditFilter';
import ResultGrid from './ResultGrid';
var EMPTY_FN = function(){};

class Batcheditstepmodal extends Component {
    constructor(props) {
        super(props);
        this.tableConfig = props.tableConfig;
        this.url = props.url;
        this.state = {
            showForm : false,
            modal:{
                show: false,
                modalDropup: true,
                size: 'xlg',
                fieldid:'batchupdatestep_modal-area'
            },
            step:{
                current: 0,
                stepCount : 3
            },
            tableItems : {},
            orgs : [],
            pks : []
        };
        this.onFinish = props.onFinish || EMPTY_FN;
    }

    show = (orgs,pks) => {
        ajax({
            url : '/nccloud/uapbd/material/queryBatchUpdateTab.do',
            data : {mdId:this.tableConfig.mdId},
            success : (res) => {
                if(res.data && res.data.tab){
                    let meta = this.props.meta.getMeta();
                    let tableItems = {};
                    res.data.tab.forEach(element => {
                        let attrItems = [];
                        tableItems[element.code] = {
                            code : element.code,
                            name : element.name,
                            attrs : element.attrs,
                            attrItems : attrItems
                        };
                        element.attrs.forEach(attr=>{
                            this.tableConfig.tableRelation[element.code].forEach(metaid=>{
                                meta[metaid].items.forEach(item=>{
                                    if(item.attrcode === attr.code){
                                        delete item.title;
                                        let i = JSON.parse(JSON.stringify(item));
                                        i.visible = true;
                                        i.disabled = false;
                                        attrItems.push(i);
                                    }else if(this.tableConfig.specialItems[element.code] && this.tableConfig.specialItems[element.code] === item.attrcode){
                                        delete item.title;
                                        tableItems[element.code].special = JSON.parse(JSON.stringify(item));
                                        tableItems[element.code].special.visible = true;
                                        tableItems[element.code].special.disabled = false;
                                        tableItems[element.code].special.isMultiSelectedEnabled = true;
                                        this.setRefcodeAttr(tableItems[element.code],element.code);
                                    }
                                });
                            });
                        });
                    });
                    this.state.tableItems = tableItems;
                    this.state.modal.show = true;
                    this.state.showForm = true;
                    this.state.orgs = orgs;
                    this.state.pks = pks;
                    this.setState(this.state);
                }
            }
        });
    }
    setRefcodeAttr = (item,tabCode)=>{
        switch(tabCode){
            case 'sup_base_info':
                item.special.refcode = 'uapbd/refer/org/BusinessUnitTreeRef/index.js';
                break;
            case 'sup_purchase':
                item.special.refcode = 'uapbd/refer/org/FinanceOrgTreeRef/index.js';
                break
            case 'sup_finance':
                item.special.refcode = 'uapbd/refer/org/PurchaseOrgGridRef/index.js';
                break;

        }

    }
    onsubmit = () => {
        let reqData = this.Batcheditform.getData();
        reqData.orgs = reqData.selectedOrgs || this.state.orgs;
        reqData.nodeType = this.props.nodeType;
        reqData.pk_curOrg = this.props.envParam.pk_org;
        let queryInfo = this.props.search.getQueryInfo(this.tableConfig.searchid);
        if(!queryInfo){return;}
        let OID = queryInfo.oid;
        // reqData.queryInfo = {
        //     pageInfo:this.props.table.getTablePageInfo(this.tableConfig.tableId),
        //     querycondition: this.props.search.getAllSearchData(this.tableConfig.searchid),
        //     oid: OID ,
        //     queryAreaCode:this.tableConfig.searchid,
        //     querytype:'tree'
        // };
        let searchData = this.props.search.getAllSearchData(this.tableConfig.searchid);
        if(!searchData){toast({content:'请设置过滤条件！',color:'warning'});return;};
        reqData.formatVO = {
            pageInfo:this.props.table.getTablePageInfo(this.tableConfig.tableId),
            querycondition: searchData,
            oid: OID ,
            queryAreaCode:this.tableConfig.searchid,
            querytype:'tree'
        };
        reqData.pks = (this.state.step.current == this.state.step.stepCount - 1)?this.resultGrid.getData():[];
        ajax({
            url : this.url,
            data : reqData,
            success : (res) => {
                this.state.modal.show = false;
                this.setState(this.state,()=>{this.onFinish(res);});
            }
        })
    }

    onStep = (dir) => {
        if(dir < 0 && this.state.step.current > 0){
            this.state.step.current = this.state.step.current -1;
            this.setState(this.state);
        }else if(dir > 0 && this.state.step.current < this.state.step.stepCount - 1){
            if(this.state.showForm && this.Batcheditform.getData()){
                if(this.state.step.current === 1){
                    let qryinfo = this.props.search.getQueryInfo(this.tableConfig.searchid);
                    if(!qryinfo) return;
                    var param  = {
                        formatInfo:{
                            pageInfo:this.props.table.getTablePageInfo('assign_supplier_baseInfo'),
                            querycondition: this.props.search.getAllSearchData('assignsupplierquery'),
                            oid: qryinfo.oid || this.props.oid,
                            queryAreaCode:'assignsupplierquery',
                            querytype:'tree'
                        },
                        areacode:'assign_supplier_baseInfo',
                        showOff:false,
                        nodeType:this.props.nodeType,
                    };
                    ajax({
                        url: this.tableConfig.queryTable,
                        data:param,
                        success : (res)=>{
                            let {sucess,data} = res;
                            if(data[this.tableConfig.tableid]){
                                this.props.table.setAllTableData(this.tableConfig.tableid,data[this.tableConfig.tableid]);
                            }
                        }
                    });
                }
                this.state.step.current = this.state.step.current + 1;
                this.setState(this.state);
            }

        }
    }

    clearDataByAreaCode = (tableId,searchId)=>{
        this.props.table.setAllTableData(tableId,{rows:[]});
        this.props.search.clearSearchArea(searchId);
        if(this.props.nodeType != 'ORG_NODE'){
            this.props.meta.getMeta()[searchId].items.find(item=>{
                if(item.attrcode.endsWith("pk_org_assign") || item.attrcode.endsWith("pk_org")){
                    let data = {value:this.props.envParam.pk_org,display:this.props.envParam.org_Name};
                    this.props.search.setSearchValByField(searchId,item.attrcode, data);
                }
            })
        }
    }
    close = ()=>{
        this.state.step.current = 0;
        this.state.modal.show = false;
        this.state.orgs = null;
        this.state.tableItems = null;
        this.state.pks = null;
        this.setState(this.state,()=>{
            this.clearDataByAreaCode(this.tableConfig.tableid,this.tableConfig.searchid);
        });
    }
    render(){

        return (
            <NCModal {...this.state.modal} fieldid="batchstep">
                <NCModal.Header closeButton={false}>
                    <NCModal.Title>向导批改</NCModal.Title>
                </NCModal.Header>
                <NCModal.Body>
                    <NCStep.NCSteps { ...this.state.step}>
                        <NCStep title="步骤1"  description="选择批改属性并设值" />
                        <NCStep title="步骤2"  description="设置过滤条件" />
                        <NCStep title="步骤3"  description="选择批改数据" />
                    </NCStep.NCSteps>
                    <div style={{height: '400px',display: this.state.step.current == 0 ? '': 'none'}}>
                        <Batcheditform tableItems={this.state.tableItems} ref={item=>this.Batcheditform=item} {...this.props} />
                    </div>
                    <div style={{height: '400px',display: this.state.step.current == 1 ? '': 'none'}}><BatchEditFilter {...this.props}/></div>
                    <div style={{height: '400px',display: this.state.step.current == 2 ? '': 'none'}}><ResultGrid ref={(item)=> this.resultGrid = item} {...this.props}/></div>
                </NCModal.Body>
                <NCModal.Footer>
                    <span style={{display: this.state.step.current == 0 ? 'none' : '' }}><NCButton onClick={ ()=>{this.onStep(-1)} }>上一步</NCButton></span>
                    <span style={{display: this.state.step.current == this.state.step.stepCount - 1  ? 'none' : '' }}><NCButton onClick={ ()=>{this.onStep(1)} }>下一步</NCButton></span>
                    <span style={{display: this.state.step.current == this.state.step.stepCount - 1 || this.state.step.current == this.state.step.stepCount - 2  ? '' : 'none' }} ><NCButton onClick={ this.onsubmit }>完成</NCButton></span>
                    <NCButton onClick={()=>{this.close()}}>关闭</NCButton>
                </NCModal.Footer>
            </NCModal>
        )
    }

}

export default Batcheditstepmodal;
/*bC6PlMPj1z4dd3YE5o/v0g0kPVRUE18Ro7GsIADPKKLPhkPmwZoPURbQgyBRC/4H*/
